/*
 * @Description: 分页处理
 * @Author: franktrue 807615827@qq.com
 * @Date: 2021-07-29 07:54:46
 * @LastEditTime: 2021-07-31 12:00:15
 */
package app

import (
	"github.com/franktrue/blog-service/global"
	"github.com/franktrue/blog-service/pkg/convert"
	"github.com/gin-gonic/gin"
)

type Pager struct {
	Page      int   `json:"page"`
	PageSize  int   `json:"page_size`
	TotalRows int64 `json:"total_row"`
}

func GetPage(c *gin.Context) int {
	page := convert.StrTo(c.Query("page")).MustInt()
	if page <= 0 {
		return 1
	}
	return page
}

func GetPageSize(c *gin.Context) int {
	pageSize := convert.StrTo(c.Query("page_size")).MustInt()
	if pageSize <= 0 {
		return global.AppSetting.DefaultPageSize
	}
	if pageSize > global.AppSetting.MaxPageSize {
		return global.AppSetting.MaxPageSize
	}
	return pageSize
}

func GetPageOffset(page, pageSize int) int {
	result := 0
	if page > 0 {
		result = (page - 1) * pageSize
	}
	return result
}
